package com.zbw.weightlibrary.WeightSub;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.example.testapplication.BLSManagerService;
import com.example.testapplication.WeightCallback;
import com.example.testapplication.WeightService;
import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 ：Wesley
 * 时间 ：2023-04-25 16:42
 * BalanceScale 说明：
 * 佰伦斯称重秤
 */
public class BalanceScale implements IBleControl {
    private BLSManagerService mService;
    private WeightService mWeightService;
    private final Context mContext;
    private Intent intent;
    private final IinfoCallback mCallback;

    public BalanceScale(Context context, IinfoCallback callback) {
        mContext = context;
        mCallback = callback;
    }

    @Override
    public void bleConnect() {
        if(mService == null){
            intent = new Intent();
            intent.setPackage("com.example.testapplication");
            intent.setAction("com.example.testapplication.services.BlsManagerService");
            mContext.bindService(intent, connService, Context.BIND_AUTO_CREATE);
        }
    }

    public ServiceConnection connService = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
            mWeightService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = BLSManagerService.Stub.asInterface(service);
            try{
                mWeightService = WeightService.Stub.asInterface(mService.getWeightService());
                if(mWeightService != null) {
                    mCallback.onConnect("绑定成功！",true);
                    mWeightService.registerWeightCallBack(weightCallback);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };


    private WeightCallback weightCallback = new WeightCallback.Stub() {

        //weightNum 重量数值   isSteady 秤盘是否稳定  isOverWeight 秤盘是否超载
        @Override
        public void onDataReceived(double weightNum, boolean isSteady, boolean isOverWeight) {
            mCallback.onGetWeight(String.valueOf(weightNum),isSteady);
        }

        @Override
        public void getDeviceInfo(String deviceInfo) throws RemoteException {
        }
    };


    @Override
    public void bleDisconnect() {
        try {
            mWeightService.cancelReadWeight();
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
        mContext.unbindService(connService);
    }

    @Override
    public void getWeight() {
        if(mWeightService != null) {
            try {
                mWeightService.readWeight();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void stopWeight() {
        if(mWeightService != null) {
            try {
                mWeightService.cancelReadWeight();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void clearTare() {

    }

    @Override
    public void tare() {
        if(mWeightService != null) {
            try {
                mWeightService.tare();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void zero() {
        if(mWeightService != null) {
            try {
                mWeightService.zero();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
